package org.xxpay.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.StatisticsPlat;
import org.xxpay.core.entity.StatisticsPlatExample;
import org.xxpay.core.service.IStatisticsPlatService;
import org.xxpay.service.common.annotation.DataSourceSwitch;
import org.xxpay.service.common.enumm.DataSourceType;
import org.xxpay.service.dao.mapper.StatisticsPlatMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 平台统计表 服务实现类
 * </p>
 *
 * @author zhuxiao
 * @since 2021-02-26
 */
@Service
public class StatisticsPlatServiceImpl implements IStatisticsPlatService {

    @Autowired
    private StatisticsPlatMapper statisticsPlatMapper;

    @Override
    public List<StatisticsPlat> select(int pageIndex, int pageSize, StatisticsPlat statisticsPlat, JSONObject queryObj) {
        StatisticsPlatExample example = new StatisticsPlatExample();
        example.setOrderByClause("statisticsDate desc");
        example.setOffset(pageIndex);
        example.setLimit(pageSize);
        StatisticsPlatExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, statisticsPlat, queryObj);
        return statisticsPlatMapper.selectByExample(example);
    }

    @Override
    public List<StatisticsPlat> select2(int pageIndex, int pageSize, StatisticsPlat statisticsPlat, Date createTimeStart, Date createTimeEnd) {
        StatisticsPlatExample example = new StatisticsPlatExample();
        example.setOrderByClause("statisticsDate desc");
        example.setOffset(pageIndex);
        example.setLimit(pageSize);
        StatisticsPlatExample.Criteria criteria = example.createCriteria();
        if (createTimeStart != null) {
            criteria.andStatisticsDateGreaterThanOrEqualTo(createTimeStart);
        }
        if (createTimeEnd != null) {
            criteria.andStatisticsDateLessThanOrEqualTo(createTimeEnd);
        }
        return statisticsPlatMapper.selectByExample(example);
    }

    @Override
    public long count(StatisticsPlat statisticsPlat, JSONObject queryObj) {
        StatisticsPlatExample example = new StatisticsPlatExample();
        StatisticsPlatExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, statisticsPlat, queryObj);
        return statisticsPlatMapper.countByExample(example);
    }

    @Override
    public long count2(StatisticsPlat statisticsPlat, Date createTimeStart, Date createTimeEnd) {
        StatisticsPlatExample example = new StatisticsPlatExample();
        StatisticsPlatExample.Criteria criteria = example.createCriteria();
        if (createTimeStart != null) {
            criteria.andStatisticsDateGreaterThanOrEqualTo(createTimeStart);
        }
        if (createTimeEnd != null) {
            criteria.andStatisticsDateLessThanOrEqualTo(createTimeEnd);
        }
        return statisticsPlatMapper.countByExample(example);
    }

    @Override
    public int add(StatisticsPlat statisticsPlat) {
        return statisticsPlatMapper.insertSelective(statisticsPlat);
    }

    @Override
    public int updateByPrimaryKey(StatisticsPlat statisticsPlat) {
        return statisticsPlatMapper.updateByPrimaryKeySelective(statisticsPlat);
    }

    @Override
    public StatisticsPlat findByPrimaryKey(Date statisticsDate) {
        return statisticsPlatMapper.selectByPrimaryKey(statisticsDate);
    }

    @Override
    public Map countAll() {
        return statisticsPlatMapper.countAll();
    }

    @Override
    @DataSourceSwitch(DataSourceType.SLAVE)
    public JSONObject payCorrect(Long mchId, Integer payPassageAccountId, Integer payPassageId, List<Byte> statusList, String statisticsDateStart, String statisticsDateEnd) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        if(payPassageAccountId != null) param.put("payPassageAccountId", payPassageAccountId);
        if(payPassageId != null) param.put("payPassageId", payPassageId);
        if(CollectionUtils.isNotEmpty(statusList)) param.put("statusList", statusList);
        if(StringUtils.isNotBlank(statisticsDateStart)) param.put("statisticsDateStart", statisticsDateStart);
        if(StringUtils.isNotBlank(statisticsDateEnd)) param.put("statisticsDateEnd", statisticsDateEnd);
        return statisticsPlatMapper.payCorrect(param);
    }

    @Override
    @DataSourceSwitch(DataSourceType.SLAVE)
    public JSONObject agentpayCorrect(Long mchId, Byte status, String statisticsDateStart, String statisticsDateEnd) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        if(status != null) param.put("status", status);
        if(StringUtils.isNotBlank(statisticsDateStart)) param.put("statisticsDateStart", statisticsDateStart);
        if(StringUtils.isNotBlank(statisticsDateEnd)) param.put("statisticsDateEnd", statisticsDateEnd);
        return statisticsPlatMapper.agentpayCorrect(param);
    }

    @Override
    @DataSourceSwitch(DataSourceType.SLAVE)
    public JSONObject rechargeCorrectSuccess(Long mchId, String statisticsDateStart, String statisticsDateEnd) {
        Map param = new HashMap<>();
        if(mchId != null) param.put("mchId", mchId);
        if(StringUtils.isNotBlank(statisticsDateStart)) param.put("statisticsDateStart", statisticsDateStart);
        if(StringUtils.isNotBlank(statisticsDateEnd)) param.put("statisticsDateEnd", statisticsDateEnd);
        return statisticsPlatMapper.rechargeCorrectSuccess(param);
    }

    @Override
    public int updateStatistics(StatisticsPlat updateRecord) {
        return statisticsPlatMapper.updateStatistics(updateRecord);
    }


    void setCriteria(StatisticsPlatExample.Criteria criteria, StatisticsPlat statisticsPlat, JSONObject queryObj) {
        if(statisticsPlat != null) {
            if(statisticsPlat.getStatisticsDate() != null) criteria.andStatisticsDateEqualTo(statisticsPlat.getStatisticsDate());
        }
        if(queryObj != null) {
            if(queryObj.getDate("createTimeStart") != null) criteria.andCreateTimeGreaterThanOrEqualTo(queryObj.getDate("createTimeStart"));
            if(queryObj.getDate("createTimeEnd") != null) criteria.andCreateTimeLessThanOrEqualTo(queryObj.getDate("createTimeEnd"));
        }
    }

}
